//
//  CTDataManager.m
//  CT
//
//  Created by liufanwei on 15/4/10.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTHistoryManager.h"
#import "CoreData+MagicalRecord.h"
#import "CTContactManager.h"

@interface CTHistoryManager(){
    NSMutableArray * _Data;
    
}
@end


@implementation CTHistoryManager

+(CTHistoryManager*)shareInstance{
    static dispatch_once_t onceToken;
    static CTHistoryManager * instance;
    dispatch_once(&onceToken, ^{
        instance = [[CTHistoryManager alloc] init];
    });
    return instance;
}

-(void)reload{
    [_Data removeAllObjects];
    NSArray * allData = [CTContactEntity MR_findAllSortedBy:@"date" ascending:NO];
    for (CTContactEntity * item in allData) {
        CTContactShowItem * showItem = [CTContactShowItem createWithEntity:item];
        CTContactShowItem * locItem = [[CTContactManager share] ContactWithPhoneNumber:showItem.PhoneNumber];
        if (locItem) {
            showItem.ImageData = locItem.ImageData;
            showItem.Name = locItem.Name;
            showItem.PhoneNumber = locItem.PhoneNumber;
        }else{
            showItem.ImageData = Nil;
            showItem.Name = showItem.PhoneNumber;
            showItem.PhoneNumber = showItem.PhoneNumber;
        }
        [_Data addObject:showItem];
    }
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _Data = [[NSMutableArray alloc] init];
    }
    return self;
}
-(NSMutableArray*)allHistory{
    return  _Data;
}

-(void)cleanHistory{
    [_Data removeAllObjects];
    [CTContactEntity MR_truncateAll];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

-(void)addItem:(CTContactShowItem*)item{
    CTContactShowItem * locItem = [[CTContactManager share] ContactWithPhoneNumber:item.PhoneNumber];
    if (locItem) {
        item.ImageData = locItem.ImageData;
        item.Name = locItem.Name;
        item.PhoneNumber = locItem.PhoneNumber;
    }else{
        item.ImageData = Nil;
        item.Name = item.PhoneNumber;
        item.PhoneNumber = item.PhoneNumber;
    }
    
    CTContactEntity *detail = [CTContactEntity MR_createEntity];
    detail.cid = item.Cid;
    detail.addr = item.Addr;
    detail.name = item.Name;
    detail.phonenumber = item.PhoneNumber;
    detail.pingying = item.PingYing;
    detail.sortstr = item.SortStr;
    detail.date = item.Date;
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    [_Data insertObject:item atIndex:0];
}

-(void)removeItem:(CTContactShowItem*)item{
    [_Data removeObject:item];
    [CTContactEntity MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"self.date == %@",item.Date]];
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

-(NSMutableArray*)allFiltrate:(NSString*)str{
    NSMutableArray * rtn = [[NSMutableArray alloc] init];
    for(CTContactShowItem * item in _Data){
        NSString * phoneNumber = item.PhoneNumber;
        if (phoneNumber == Nil || phoneNumber.length == 0) {
            continue;
        }
        NSRange range = [phoneNumber rangeOfString:str];
        if (range.length != 0) {
            [rtn addObject:item];
            continue;
        }
        NSString * name = item.Name;
        range = [name rangeOfString:str];
        if (range.length != 0) {
            [rtn addObject:item];
            continue;
        }
        NSString * pinyin = item.PingYing;
        range = [pinyin rangeOfString:str];
        if (range.length != 0) {
            [rtn addObject:item];
            continue;
        }
    }
    return rtn;
}

@end







